package com.taomu.app.zongchouhuli.moudle.index.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseFragment;
import com.taomu.app.zongchouhuli.framework.BasePresenter;
import com.taomu.app.zongchouhuli.framework.CommonWebActivity;
import com.taomu.app.zongchouhuli.http.res.ResContractInfo;
import com.taomu.app.zongchouhuli.http.res.ResLevenlEntity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.index.contract.MineContract;
import com.taomu.app.zongchouhuli.moudle.index.contract.impl.MinePresenter;
import com.taomu.app.zongchouhuli.moudle.login.activity.LoginActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserInfoActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserMemberActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserOrderActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserRebateListActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserSettingAcitivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserSpreadActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserSpreadCodeActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserWalletActivity;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;
import com.taomu.app.zongchouhuli.utils.TokenDialog;
import com.taomu.app.zongchouhuli.utils.zxing.QrUtils;
import com.taomu.app.zongchouhuli.weight.LineViewText;

/**
 * 我的首页
 */
public class MineIndexFragment extends BaseFragment<MineContract.Presenter> implements MineContract.View{

    private SmartRefreshLayout smartRefreshLayout;

    private ImageView mIconIm;

    private ImageView mQrIm;

    private TextView mNickNameTv;

    private TextView mPriceTv;

    private LinearLayout mRebateLin;

    private LinearLayout mSpreadLin;

    private LinearLayout mOrderLin;

    private LineViewText mMemberLvt;

    private LineViewText mWalletLvt;

    private LineViewText mSettingLvt;

    private LineViewText mAboutLvt;

    private LineViewText mQQLvt;

    private LineViewText mWechatLvt;

    private RelativeLayout mBaseInfoRel;

    private RelativeLayout mLoginRel;

    private ImageView mSexIm;

    public static MineIndexFragment getInstence(){
        return new MineIndexFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine_fragment;
    }

    @Override
    protected void initView(View view) {
        smartRefreshLayout = view.findViewById(R.id.smart_refresh);
        smartRefreshLayout.setEnableLoadMore(false);
        mIconIm = view.findViewById(R.id.mine_icon_im);
        mQrIm = view.findViewById(R.id.mine_qr_im);
        mNickNameTv = view.findViewById(R.id.mine_account_tv);
        mSexIm = view.findViewById(R.id.mine_sex_im);
        mPriceTv = view.findViewById(R.id.mine_price_tv);
        mRebateLin = view.findViewById(R.id.mine_rebate_lin);
        mSpreadLin = view.findViewById(R.id.mine_spread_lin);
        mOrderLin = view.findViewById(R.id.mine_order_lin);
        mMemberLvt = view.findViewById(R.id.mine_member_lvt);
        mWalletLvt = view.findViewById(R.id.mine_wallet_lvt);
        mSettingLvt = view.findViewById(R.id.mine_setting_lvt);
        mAboutLvt = view.findViewById(R.id.mine_about_lvt);
        mQQLvt = view.findViewById(R.id.mine_service_qq_lvt);
        mWechatLvt = view.findViewById(R.id.mine_service_wechat_lvt);
        mLoginRel = view.findViewById(R.id.mine_login_rel);
        mBaseInfoRel = view.findViewById(R.id.mine_login_base_info_rel);
    }

    @Override
    protected void initData() {
        mMemberLvt.setText("");
        mQQLvt.setText("");
        mWechatLvt.setText("");
    }

    @Override
    protected void initEvent() {
        smartRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if(StorageHelper.isLogin()) {
                    mPresenter.getLevenl();
                    mPresenter.getQQInfo();
                    mPresenter.getWeiXinInfo();
                    mPresenter.getUserInfo();
                }
            }
        });
        mIconIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), UserInfoActivity.class);
                startActivity(intent);
            }
        });
        mLoginRel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
            }
        });
        mSettingLvt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(),UserSettingAcitivity.class);
                startActivity(intent);
            }
        });
        mMemberLvt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StorageHelper.isLogin()) {
                    Intent intent = new Intent(getContext(), UserMemberActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        mWalletLvt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StorageHelper.isLogin()) {
                    Intent intent = new Intent(getContext(), UserWalletActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        mRebateLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StorageHelper.isLogin()) {
                    Intent intent = new Intent(getContext(), UserRebateListActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        mSpreadLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StorageHelper.isLogin()) {
                    Intent intent = new Intent(getContext(), UserSpreadActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        mOrderLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StorageHelper.isLogin()) {
                    Intent intent = new Intent(getContext(), UserOrderActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        mQrIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(),UserSpreadCodeActivity.class);
                startActivity(intent);
            }
        });
        mAboutLvt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StorageHelper.isLogin()) {
                    mPresenter.getArticleInfo();
                }else{
                    Intent intent = new Intent(getContext(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected MineContract.Presenter createPresenter() {
        return new MinePresenter(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("dwqdwqdwqdwqdqwd", "onResume");
        showBaseInfo();
        if(StorageHelper.isLogin()) {
            mPresenter.getLevenl();
            mPresenter.getQQInfo();
            mPresenter.getWeiXinInfo();
            mPresenter.getUserInfo();
            smartRefreshLayout.setEnableRefresh(true);
        }else{
            smartRefreshLayout.setEnableRefresh(false);
        }
    }

    private void showBaseInfo(){
        if(StorageHelper.isLogin()){
            //组装参数
            mBaseInfoRel.setVisibility(View.VISIBLE);
            mLoginRel.setVisibility(View.GONE);
            mNickNameTv.setText(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getUsername());
            mPriceTv.setText("¥ "+ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getAccount());
            Glide.with(this).load(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getUser_img_uri()).into(mIconIm);
            Bitmap barcodebit = QrUtils.createQr(String.valueOf(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getInvite_code()),48);
            if(barcodebit != null){
                mQrIm.setImageBitmap(barcodebit);
            }
            if(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getSex().equals("0")){
                mSexIm.setImageResource(R.drawable.icon_boy);
            }else{
                mSexIm.setImageResource(R.drawable.icon_girl);
            }
        }else{
            mBaseInfoRel.setVisibility(View.GONE);
            mLoginRel.setVisibility(View.VISIBLE);
            mQrIm.setVisibility(View.GONE);
            mMemberLvt.setText("");
        }
    }

    @Override
    public void setWeiXinInfo(ResContractInfo entity) {
        mWechatLvt.setText(entity.getValue());
    }

    @Override
    public void setQQInfo(ResContractInfo entity) {
        mQQLvt.setText(entity.getValue());
    }

    @Override
    public void setLevenl(ResLevenlEntity entity) {
        mMemberLvt.setText(entity.getName());
    }

    @Override
    public void setArticleInfo(String htmlstr) {
        CommonWebActivity.startWithContent(getActivity(),htmlstr,"关于我们");
    }

    @Override
    public void setUserInfo(ResUserInfoEntity entity) {
        smartRefreshLayout.finishRefresh();
        mBaseInfoRel.setVisibility(View.VISIBLE);
        mLoginRel.setVisibility(View.GONE);
        mNickNameTv.setText(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getUsername());
        mPriceTv.setText("¥ "+ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getAccount());
        Glide.with(this).load(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getUser_img_uri()).into(mIconIm);
        Bitmap barcodebit = QrUtils.createQr(String.valueOf(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getInvite_code()),48);
        if(barcodebit != null){
            mQrIm.setImageBitmap(barcodebit);
        }
        if(!TextUtils.isEmpty(StorageHelper.getAccountInfo())) {
            if (ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getSex().equals("0")) {
                mSexIm.setImageResource(R.drawable.icon_boy);
            } else {
                mSexIm.setImageResource(R.drawable.icon_girl);
            }
        }
    }

    @Override
    public void setPresenter(MineContract.Presenter presenter) {

    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToken() {
        TokenDialog.getInsence(getContext()).show();
    }

}
